<?php

/**
 * SimplifyPHP Framework
 *
 * This file is part of SimplifyPHP Framework.
 *
 * SimplifyPHP Framework is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * SimplifyPHP Framework is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>
 */

/**
 *
 * @author "Rodrigo Rutkoski Rodrigues, <rutkoski@gmail.com>"
 *
 */
class DataMapper
{

  /**
   *
   * @param string $class
   * @param array $row
   * @param DomainDefinition $config
   * @return Entity
   */
  public static function toEntity($class, $row, DomainDefinition $config = null)
  {
    if (class_exists($class)) {
      $obj = new $class();
    }
    else {
      $obj = new CustomEntity($class, $config->getEntity($class));
    }

    DataMapper::inflate($row, $obj->getDefinition(), $obj);

    return $obj;
  }

  /**
   * Inflate a collection of objects
   *
   * @param array $data
   * @param $fields
   * @param string $class
   * @return array
   */
  public static function populate(array $data, $fields, $class = null)
  {
    $c = array();

    foreach ($data as $row) {
      $to = $class ? new $class() : null;
      $c[] = self::inflate($row, $fields, $to);
    }

    return $c;
  }

  /**
   *
   * @param array|object $from
   * @param $fields
   * @param array|object $to
   * @return mixed
   */
  public static function inflate($from, EntityDefinition $def, $to = array())
  {
    $fields = $def->getFields();

    foreach ($fields as $name => $field) {
      $value = is_array($from) ? $from[$field->getName()] : $from->{$field->getName()};
      $value = self::inflateValue($value, $field->getType());

      if (is_array($to)) {
        $to[$name] = $value;
      }
      else {
        $to->$name = $value;
      }
    }

    return $to;
  }

  /**
   *
   * @param array|object $from
   * @param $fields
   * @param array|object $to
   * @return unknown_type
   */
  public static function deflate($from, EntityDefinition $def, $to = array())
  {
    $fields = $def->getFields();

    foreach ($fields as $name => $params) {
      $value = sy_get_param($from, $name);

      $value = self::deflateValue($value, $params->getType());

      sy_set_param($to, $params->getName(), $value);
    }

    return $to;
  }

  /**
   * Convert from sql value to php value
   *
   * @param mixed $value
   * @param string $type
   * @return mixed
   */
  public static function inflateValue($value, $type)
  {
    switch ($type) {
      case 'decimal' :
      case 'float' :
        $value = ( float ) $value;
        break;

      case 'boolean' :
        $value = ( bool ) $value;
        break;

      case 'integer' :
        $value = ( int ) $value;
        break;

      case 'date' :
      case 'time' :
      case 'datetime' :
      case 'timestamp' :
        $value = new DateTime($value);
        break;

      case 'text' :
      default :
        $value = $value;
    }

    return $value;
  }

  /**
   * Convert from php value to sql value
   *
   * @param mixed $value
   * @param string $type
   * @return mixed
   */
  public static function deflateValue($value, $type)
  {
    switch ($type) {
      case 'decimal' :
      case 'float' :
        $value = ( float ) $value;
        break;

      case 'boolean' :
        $value = $value ? 1 : 0;
        break;

      case 'integer' :
        $value = ( int ) $value;
        break;

      case 'date' :
        $value = is_a($value, 'DateTime') ? $value->format('Y-m-d') : '0000-00-00';
        break;

      case 'time' :
        $value = is_a($value, 'DateTime') ? $value->format('H:i:s') : '00:00:00';
        break;

      case 'datetime' :
      case 'timestamp' :
        $value = is_a($value, 'DateTime') ? $value->format('Y-m-d H:i:s') : '0000-00-00 00:00:00';
        break;

      case 'text' :
      default :
        $value = ( string ) $value;
    }

    return $value;
  }

}

?>